package interchange.data;

import interchange.domain.Intercambio;
import interchange.domain.Puntuacion;
import interchange.domain.Subasta;
import interchange.domain.Usuario;
import interchange.utiles.UIDGenerator;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class JDBCPuntuacionDAO implements IPuntuacionDAO{
	private Connection conn;
	private IUsuarioDAO udao;
	
	public JDBCPuntuacionDAO() {
        conn = ConnectionManager.getInstance().checkOut();
        udao = new JDBCUsuarioDAO();
        
    }
	
	 protected void finalize() {
	        ConnectionManager.getInstance().checkIn(conn);
	    }
	 
	public void insert(Connection conn, String OIDPuntuacion, Puntuacion p) {
		String oids = UIDGenerator.getInstance().getKey();
		
		String oidu = UIDGenerator.getInstance().getKey();
        udao.insert(conn, oidu,p.getUsuarioAPuntuar() );
		
		
		String sql="INSERT INTO puntuacion(OIDPuntuacion,OIDIntercambio,idPuntuacion,usuarioAPuntuar,puntuacion) VALUES (?,?,?,?,?)";
		PreparedStatement stmt = null;
		try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oids);
            stmt.setString(2, oidu);
            stmt.setString(3, p.getidPuntuacion());
            stmt.setUsuario(4, p.getUsuarioAPuntuar());
            stmt.setDouble(5, p.getPuntuacion());
           
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

	}


	
	
	public Puntuacion select(Connection conn, String OIDPuntuacion) {
		String sql="SELECT * FROM puntuacion WHERE (OIDPuntuacion=?)";
		PreparedStatement stmt = null;
        ResultSet result = null;
        Puntuacion p = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, OIDPuntuacion);

            result = stmt.executeQuery();

            result.next();
            
            String oido = result.getString("OIDIntercambio");
            
            p = (Puntuacion) new Puntuacion();
            p.setPuntuacion(new Double(result.getString("puntuacion")));
            p.setUsuarioAPuntuar(udao.select(conn, iodu));
        
        
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return p;
    }

}
	